<?php
class AdvertisingController extends Controller {
	public function index() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'ad' . DS . 'AdvertisingService.class.php';
		if (! CommonBase::isPost ()) {
			$this->smarty->display ( "admin/ad/advertising.tpl" );
			return;
		}
		// START 数据库查询及分页数据
		$imgService = new AdvertisingService ( $this->getDB () );
		$page_size = 2;
		$page_no = $page_no < 1 ? 1 : $page_no;
		$row_count = $imgService->countNum ();
		$total_page = $row_count / $page_size == 0 ? $row_count / $page_size : ceil ( $row_count / $page_size );
		$total_page = $total_page < 1 ? 1 : $total_page;
		$page_no = $page_no > ($total_page) ? ($total_page) : $page_no;
		$start = ($page_no - 1) * $page_size;
		
		$user_infos = $imgService->userPage ( $start, $page_size );
		$this->smarty->assign ( 'user_infos', $user_infos );
		$this->smarty->assign ( '_GET', $_POST );
		// $this->smarty->assign ( 'page_no', $page_no );
		// $this->smarty->assign ( 'page_html', $page_html );
		$this->smarty->display ( "admin/ad/advertising.tpl" );
	}
	public function getContent() {
		$this->getSmarty ();
		$type = $_POST ["type"];
		$user = $_SESSION ['user'];
		
		if (empty ( $user )) {
			$condition = $type.'游客';
		} else {
			$condition = $type.'会员';
		}
		$content = $this->getDB ()->get_row ( "SELECT c.* FROM ad_content c LEFT JOIN ad_sites s ON c.ad_site_id = s.site_id WHERE s.site_name = '".$condition."'  ORDER BY c.ad_c_date desc" );
		if(empty($content)){
			$result->type = 0;
			echo json_encode($result);
			return;
		}else{
			$result->type = 1;
		}
		$rms = '<a href="' . $content->ad_href . '">';
		if ($content->ad_file_type == 0) {
			$rms .= '<img src="' . WEBSITE_URL . 'upload/' . $content->ad_file_location . '"/></a>';
		} else {
			$rms .= '<img src="' . $content->ad_file_location . '"/></a>';
		}
		$result->result = $rms;
		echo json_encode($result);
	}
	 
	public function add() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'ad' . DS . 'AdvertisingService.class.php';
		if (! CommonBase::isPost ()) {
			$this->smarty->display ( "admin/ad/advertising.tpl" );
			return;
		}
		
		$imgService = new AdvertisingService ( $this->getDB () );
		
		// ad_description fff
		// ad_file_location 2013-11-14\2013-11-14_22-02-19.jpg
		// ad_file_type 0
		// ad_href ff
		// ad_id
		// ad_index 2
		// ad_title fq
		// site_id 3
		$date = date ( "Y-m-d H-i-s" );
		$id = $_POST ["ad_id"];
		if (empty ( $id )) {
			
			$input_data = array (
					'ad_id' => md5 ( uniqid () ),
					'ad_site_id' => $_POST ["ad_site_id"],
					'ad_title' => $_POST ["ad_title"],
					'ad_href' => $_POST ["ad_href"],
					'ad_file_location' => $_POST ["ad_file_location"],
					'ad_description' => $_POST ["ad_description"],
					'ad_file_type' => $_POST ["ad_file_type"],
					'ad_index' => $_POST ["ad_index"],
					'ad_c_date' => $date,
					'ad_u_date' => $date 
			)
			;
			$data = $imgService->addItem ( $input_data );
		} else {
			$item = $imgService->getAdminByID ( $id );
			$oldimg = $_POST ["ad_file_location"];
			if ($oldimg == $item->ad_file_location) {
			} else {
				$dirroot = "uploads/";
				unlink ( $dirroot . $item->ad_file_location );
			}
			$input_data = array (
					'ad_site_id' => $_POST ["ad_site_id"],
					'ad_title' => $_POST ["ad_title"],
					'ad_href' => $_POST ["ad_href"],
					'ad_file_location' => $_POST ["ad_file_location"],
					'ad_description' => $_POST ["ad_description"],
					'ad_file_type' => $_POST ["ad_file_type"],
					'ad_index' => $_POST ["ad_index"],
					'ad_u_date' => $date 
			);
			
			$input_condition = array (
					'ad_id' => $_POST ["ad_id"] 
			);
			$data = $imgService->edit ( $input_data, $input_condition );
		}
		echo "1";
	}
	public function delete() {
		$dirroot = "uploads/arousel/";
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'ad' . DS . 'AdvertisingService.class.php';
		$imgService = new AdvertisingService ( $this->getDB () );
		$id = $_POST ['id'];
		$item = $imgService->getAdminByID ( $id );
		if (file_exists ( $dirroot . $item->location )) {
			unlink ( $dirroot . $item->location );
		}
		$user_id = $imgService->deleteItem ( $id );
		echo 1;
	}
	function test($username, $password) {
		echo 'this is a test function ,username:' . $username . ',password:' . $password;
	}
	function getpage() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'ad' . DS . 'AdvertisingService.class.php';
		// START 数据库查询及分页数据
		$imgService = new AdvertisingService ( $this->getDB () );
		
		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$offset = ($page - 1) * $rows;
		$result = array ();
		$row_count = $imgService->countNum ();
		$result ["total"] = $row_count;
		$user_infos = $imgService->userPage ( $offset, $rows );
		$items = array ();
		// while($row = $user_infos->fetch_assoc()){
		// print ($row);
		// array_push($items, $row);
		// }
		$result ["rows"] = $user_infos;
		echo json_encode ( $result );
		// $this->smarty->display("admin/user_list.tpl");
	}
}